package projet;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JFrame;

public class FenetreListe extends JFrame{
	
	private Panneau fond = new Panneau("images.jpg");
	
	public FenetreListe(String nom){
		
		// definition fenetre
			this.setContentPane(fond);
			this.setTitle("Liste des Sujets");
			this.setSize(400,250);
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setLocationRelativeTo(null); 
			this.setResizable(false);
			defListe(nom);
			
}
	public void defListe(String nom){
		 String url = "jdbc:mysql://localhost:3306/projet";
	      String user = "root";
	      String mdp = "";
	      
	      
		try {
			
		      Class.forName("com.mysql.jdbc.Driver").newInstance();
		    
		      Connection conn = DriverManager.getConnection(url, user, mdp);
		         
		      Statement state = conn.createStatement();
		      
		      String sql = "SELECT DISTINCT sujet FROM sujetco WHERE sujetco.categorie = '"+nom+ "'";
		      
		      ResultSet result = state.executeQuery(sql);
		      
		      int i =20;
		      
		      while(result.next()){
		    	  String nomC = result.getString(1);
		    	  Texte nameC = new Texte(nomC);
		    	  fond.add(nameC);
		    	  nameC.setBounds(50, 50+i , 105, 15);
		    	  i = i+30;
		    	  actioLabel(nameC);
		      }
		      
		      result.close();
		      conn.close();
		      state.close();
		         
		    } catch (Exception e) {
		      e.printStackTrace();
		    }      
	}
	 
	  public void actioLabel(Texte label){
			label.addMouseListener(new MouseListener() {
		   	 
				@Override
				public void mouseClicked(MouseEvent arg0) {
			
					
				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					
					
				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					
					
				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					
					label.setForeground(Color.blue);
					FenetreSujet fs = new FenetreSujet(label.getText());
					setVisible(false);
				}

				@Override
				public void mouseReleased(MouseEvent arg0) {
					
					
				}
					
				});
			}
}